<?php

include("setup.php");
$setup = new setup();

$cat = $_GET['category'];	//pairnei apo to ajax tis times pou edwse o xristis 
$loc = $_GET['location'];
$selected_date = $_GET['date'];


if(!isset($_GET['category'])) {
	$cat=0;
}
if(!isset($_GET['location'])) {
	$loc=0;
}
	
	//Methodos gia xtisimo tou query analoga tou se posa pedia edwse timi
	$postfields = array($cat,$loc);
	$dbfields = array('ad_catid','ad_locid');
	$qryfields = array();
	$i=0; 

	//Methodos gia xtisimo tou query analoga tou se posa pedia edwse timi
	foreach ($postfields as $pf) {
		if($pf!=0) {

			array_push($qryfields,"$dbfields[$i]=".$pf);

		}
		$i++;
	}

	//Methodos gia xtisimo tou query analoga tou se posa pedia edwse timi
	if($cat==0 && $loc==0){

		$result = $setup->qry("Select * from ADS where ad_isAproved=1");

	}else{

		$result = $setup->qry("Select * from ADS where " . join(" AND ",$qryfields)." AND ad_isAproved=1");
		
	}
	?>
	<?php
	while($row=mysql_fetch_assoc($result)) {	

		//elegxos twn imerominiwn pou epelekse apo to calendar kai apo tin imera dimosieusis tis aggelias
		$adv_date = date("d-m-Y",strtotime($row['ad_date']));
		$adv_date = strtotime($adv_date);
		$selected_date = strtotime($selected_date);


		if($adv_date < $selected_date){
			
			echo "Δεν βρέθηκαν αποτελέσματα αναζήτησης";

		}else{

		$result1 = $setup->qry("Select * from LOCATION where loc_id=".$row['ad_locid']);
		$row1 = mysql_fetch_assoc($result1);

		$result2 = $setup->qry("Select * from CATEGORIES where cat_id=".$row['ad_catid']);
		$row2 = mysql_fetch_assoc($result2);
		
		?>

		<div id="searchadv">
		<a href="portal.php?action=search&advno=<?php echo $row['ad_id']; ?>"><div id="advtitle"> <?php echo $row['ad_title']; ?> </div></a>
			
			<?php  
			if(!isset($_SESSION['username'])) {
			?>	

			<?php 
			}else { ?>

				<?php 
					$resfavorite = $setup->qry("Select * from FAVOURITES where fav_userid=".$_SESSION['userid']." and fav_adid=".$row['ad_id']);
					if(mysql_num_rows($resfavorite) == 0) { ?>

						<div id="favorite<?php echo $row['ad_id']; ?>" class="nofavorite" title="favorite" onclick="makefavorite(<?php echo $row['ad_id']; ?>);">&nbsp;</div><br>

					<?php 
					}else { ?>

						<div id="favorite<?php echo $row['ad_id']; ?>" class="favorite" title="favorite" onclick="removefavorite(<?php echo $row['ad_id']; ?>);">&nbsp;</div><br>

					<?php
					}
					?>
					

			<?php 
			}
			?>	
			<div class="advdesc"> Απασχόληση: <?php echo $row['ad_type']; ?> </div>
			<div class="advdesc"> Περιοχή: <?php echo $row1['loc_locality']." - ".$row1['loc_administrative_area_level_1']; ?></div>
			<div class="advdesc"> Κατηγορία: <?php echo $row2['cat_desc']; ?> </div>
			<a href="portal.php?action=search&advno=<?php echo $row['ad_id']; ?>"><div class="advdesc"> Email εταιρίας: <?php echo $row['ad_email']; ?> </div></a>
			<a href="portal.php?action=search&advno=<?php echo $row['ad_id']; ?>"><div class="advlastdesc"> Ημερομηνία: <?php echo date("d-m-Y",strtotime($row['ad_date'])); ?> </div></a>
		</div>
		<?php
			}
		}
		?>